<?php

namespace App\Http\Controllers\H5;

use App\Common\Utils\Helper;
use App\Http\Controllers\Controller;
use App\Models\CommodityActivity;
use App\Models\Order;
use App\Services\Commodity;
use App\Services\SupplierService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Str;

class GuestController extends Controller
{
    /**
     * 商品列表
     * @param $request
     * @return mixed
     */
    public function commodityList(Request $request)
    {
        return $this->response->array(Commodity::getActivityListWithCache($request, $request->get('type', CommodityActivity::TYPE_LIVE_TODAY)));
    }

    /**
     * 商品详情
     * @param $id
     * @return mixed
     */
    public function commodityInfo($id)
    {
        return $this->response->array(Commodity::getActivityInfoWithCache($id));
    }

    /**
     * 客户订单查询
     * @param Request $request
     * @return mixed
     */
    public function clientOrder(Request $request)
    {
        if (empty($request->get('search_name'))) {
            return $this->response->array(new LengthAwarePaginator([], 0, 15));
        }
        if ($request->get('search_name') == '13860488218'){
            return $this->response->array(new LengthAwarePaginator([], 0, 15));
        }

        $paginate = Order::query()
            ->with(['courierInformation.courierCompany'])
            ->when($request->get('search_name'), function (Builder $builder, $searchName) {
                $builder->where(function (Builder $builder) use ($searchName) {
                    if (is_numeric($searchName)) {
                        $builder->where('client_phone', $searchName);
                    } else {
                        $builder->where('client_name', $searchName);
                    }
                });
            })
            ->when($request->get('begin_date'), function (Builder $builder, $beginDate) {
                $builder->where('order_date', '>=', $beginDate);
            })
            ->when($request->get('end_date'), function (Builder $builder, $endDate) {
                $builder->where('order_date', '<=', $endDate);
            })
            ->where('status', Order::STATUS_SHIPPED)
            ->orderBy('created_at', 'desc')
            ->simplePaginate(null, ['id', 'created_at', 'client_name', 'client_phone', 'commodity_name']);

        return $this->response->array(
            Helper::dealWithNotCompatibleSimplePaginate(
                $paginate->setCollection(
                    $paginate->getCollection()->each(function ($order) {
                        $order->client_name = Str::substr($order->client_name, 0, 1) . '**';
                        $order->client_phone = Str::substr($order->client_phone, 0, 3) . '****' . Str::substr($order->client_phone, -3);
                    })
                )
            )
        );
    }

    /**
     * 创建供应商
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function storeSupplier(Request $request)
    {
        return $this->response->created(null, (new SupplierService())->store($request));
    }
}
